package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.mpandroidchart.MpChartHelper;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.MeHistory;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.StatisticsTwoPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IStatisticsTwoAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 作者：Administrator on 2017/7/1 15:15
 * 邮箱：zhanghuaiha@gmail.com
 *  个人历史记录
 */

public class StatisticsTwoActivity extends BaseActivity<IStatisticsTwoAc,StatisticsTwoPresenter> implements IStatisticsTwoAc {
    private LineChart mChart;
    private ImageView back,icon;
    private TextView title,start,end,sta_date;
    private RecyclerView top;
    private TimePickerView pvTime;
    private String udid;
    private static final String TAG = StatisticsTwoActivity.class.getSimpleName();

    @Override
    public void initView() {
        mChart = (LineChart) findViewById(R.id.sta_chart);
        back = (ImageView) findViewById(R.id.sta_back);
        icon = (ImageView) findViewById(R.id.sta_icon);
        title = (TextView) findViewById(R.id.sta_title);
        start = (TextView) findViewById(R.id.sta_start);
        end = (TextView) findViewById(R.id.sta_end);
        sta_date = (TextView) findViewById(R.id.sta_date);
        top = (RecyclerView) findViewById(R.id.sta_top);
        end.setText(DateUtils.getNextDay(System.currentTimeMillis(),"yyyy-MM-dd",1));
        start.setText(DateUtils.getNextDay(System.currentTimeMillis(),"yyyy-MM-dd",8));
        initTimePicker();
    }

    @Override
    public void initData() {
        udid = ObtainUdis.getUDID(StatisticsTwoActivity.class);
        mPresenter.loadContacts(udid);
    }

    @Override
    public void initListener() {
        String msg = "1、达标\r\n0 没有达标 1已经达标\r\n2、日均涨粉\r\n计算的是选中的日期范围内，平均每天的\r\n3、设备使用率\r\n计算的是选中的日期范围内，平均每天的\r\n4、单日均涨粉\r\n单店日均涨粉\r\n5、使用设备\r\n当天使用的设备\r\n6、使用设备\r\n当天使用的设备\r\n7、达标\r\n达标店铺数目\r\n8、使用率\r\n设备使用率";
        start.setOnClickListener(v -> pvTime.show(v));
        end.setOnClickListener(v -> pvTime.show(v));
        icon.setOnClickListener(v -> mPresenter.Dialog("提示",msg));
        back.setOnClickListener(v -> finish());

        sta_date.setOnClickListener(v -> {
            if (top.getVisibility() == View.INVISIBLE) {
                top.setVisibility(View.VISIBLE);
            }else {
                top.setVisibility(View.INVISIBLE);
            }
        });

    }

    public void showDialog(){
      //  mPresenter.Dialog("日均涨粉："+zhangfen,"设备使用率："+shiyong);
    }

    //显示隐藏某个线条
    public void visi(int position){
        List<ILineDataSet> dataSets = mChart.getData().getDataSets();
        if (dataSets.get(position).isVisible()) {
            dataSets.get(position).setVisible(false);
        }else {
            dataSets.get(position).setVisible(true);
        }
        mChart.invalidate();
    }

    private List<List<Float>> yAxisValues;
    private List<String> titles;
    private List<String> xAxisValues;

    public static final int[] PIE_COLORS = {
            Color.parseColor("#5fa1c4"),Color.parseColor("#5f79c4"), Color.parseColor("#c45f5f"),
            Color.parseColor("#b25fc4"), Color.parseColor("#9dc45f"), Color.parseColor("#805fc487"),
            Color.parseColor("#80c4b05f"), Color.parseColor("#80c4190f"), Color.parseColor("#3cb371"),
            Color.parseColor("#cd5c5c"), Color.parseColor("#66ffff"), Color.parseColor("#6600ff"),
            Color.parseColor("#660099"), Color.parseColor("#FFB6C1")
    };


    // 这里是初始化线条的x y值 所以这里可以自己实现根据实际数据
    public void setData(List<MeHistory> data) {
        if (data.size() == 0) {
            DialogUtils.TSDialog(StatisticsTwoActivity.this,"暂无数据",true);
            return;
        }
        mChart.clear();
        mChart.setData(new LineData());
        mChart.invalidate();

        yAxisValues = new ArrayList<>();
        xAxisValues = new ArrayList<>();
        titles = new ArrayList<>();
        //单台日均新增涨粉数（总新增除以总设备数）sub_count/use_charging_num
        List<Float> yAxisValues1 = new ArrayList<>();
        //单店每日新增涨粉数（总新增除以总店铺） sub_count/store_num
        List<Float> yAxisValues2 = new ArrayList<>();
        //已铺设店铺
        List<Float> yAxisValues3 = new ArrayList<>();
        //已铺设设备
        List<Float> yAxisValues4 = new ArrayList<>();
        //当日新增
        List<Float> yAxisValues5 = new ArrayList<>();
        //使用设备
        List<Float> yAxisValues6 = new ArrayList<>();
        //达标门店
        List<Float> yAxisValues7 = new ArrayList<>();
        //设备使用率
        List<Float> yAxisValues8 = new ArrayList<>();
        //wifi_num wifi设备
        List<Float> yAxisValues9 = new ArrayList<>();
        //wifi_sub_count
        List<Float> yAxisValues10 = new ArrayList<>();
        //wifi_used_num
        List<Float> yAxisValues11 = new ArrayList<>();
        //mima_num
        List<Float> yAxisValues12 = new ArrayList<>();
        //mima_sub_count
        List<Float> yAxisValues13 = new ArrayList<>();
        //mima_used_num
        List<Float> yAxisValues14 = new ArrayList<>();


        for (int i = 0; i < data.size(); i++) {
            xAxisValues.add(String.valueOf(i));
            try {
                float a = Float.parseFloat(data.get(i).getSub_count()) / Float.parseFloat(data.get(i).getUse_charging_num());
                yAxisValues1.add(a);
            } catch (Exception e) {
                yAxisValues1.add((float) 0);
            }

            try {
                float b = Float.parseFloat(data.get(i).getSub_count()) / Float.parseFloat(data.get(i).getStore_num());
                yAxisValues2.add(b);
            } catch (Exception e) {
                yAxisValues2.add((float) 0);
            }

            try {
                float c = Float.parseFloat(data.get(i).getUse_charging_num()) / Float.parseFloat(data.get(i).getCharging_num());
                yAxisValues8.add(c);
            } catch (Exception e) {
                yAxisValues8.add((float) 0);
            }

            yAxisValues3.add(Float.parseFloat(data.get(i).getStore_num()));
            yAxisValues4.add(Float.parseFloat(data.get(i).getCharging_num()));
            yAxisValues5.add(Float.parseFloat(data.get(i).getSub_count()));
            yAxisValues6.add(Float.parseFloat(data.get(i).getUse_charging_num()));
            yAxisValues7.add(Float.parseFloat(data.get(i).getOk_store_num()));
            yAxisValues9.add(Float.parseFloat(data.get(i).getWifi_num()));
            yAxisValues10.add(Float.parseFloat(data.get(i).getWifi_sub_count()));
            yAxisValues11.add(Float.parseFloat(data.get(i).getWifi_used_num()));
            yAxisValues12.add(Float.parseFloat(data.get(i).getMima_num()));
            yAxisValues13.add(Float.parseFloat(data.get(i).getMima_sub_count()));
            yAxisValues14.add(Float.parseFloat(data.get(i).getMima_used_num()));

        }

        yAxisValues.add(yAxisValues1);
        yAxisValues.add(yAxisValues2);
        yAxisValues.add(yAxisValues3);
        yAxisValues.add(yAxisValues4);
        yAxisValues.add(yAxisValues5);
        yAxisValues.add(yAxisValues6);
        yAxisValues.add(yAxisValues7);
        yAxisValues.add(yAxisValues8);
        yAxisValues.add(yAxisValues9);
        yAxisValues.add(yAxisValues10);
        yAxisValues.add(yAxisValues11);
        yAxisValues.add(yAxisValues12);
        yAxisValues.add(yAxisValues13);
        yAxisValues.add(yAxisValues14);

        titles.add("单台日均新增涨粉数");
        titles.add("单店每日新增涨粉数");
        titles.add("已铺设店铺");
        titles.add("已铺设设备");
        titles.add("当日新增");
        titles.add("使用设备");
        titles.add("达标门店");
        titles.add("使用率");
        titles.add("wifi设备");
        titles.add("wifi涨粉");
        titles.add("wifi使用");
        titles.add("密码设备");
        titles.add("密码涨粉");
        titles.add("密码使用");

        MpChartHelper.setLinesChart(mChart, xAxisValues, yAxisValues, titles, true, PIE_COLORS);
        mChart.notifyDataSetChanged();
    }

    /**
     * 初始化时间选着器
     */
    private void initTimePicker() {

        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                long start_time = date.getTime();
                long end_time = System.currentTimeMillis();
                if (start_time > end_time) {
                    ToastUtil.showToast("选择日期不能大于当前时间");
                    return;
                }
                String time = getTime(date);
                TextView textView = (TextView) v;
                //开始时间变化
                if (textView.getId() == R.id.sta_start) {
                    long time_long = getTime_long(end.getText().toString());
                    Log.e(TAG,"start被点击了"+time_long);
                    if (start_time < time_long) {
                        mPresenter.loadNetWork(udid,time,end.getText().toString());
                    }else {
                        ToastUtil.showToast("开始时间大于结束时间");
                        return;
                    }

                }else if (textView.getId() == R.id.sta_end) {
                    long time_long = getTime_long(start.getText().toString());
                    if (time_long < start_time) {
                        mPresenter.loadNetWork(udid,start.getText().toString(),time);
                    }else {
                        ToastUtil.showToast("结束时间必须大于开始时间");
                    }
                }
                textView.setText(time);


            }
        })
                .build();
        pvTime.setDate(Calendar.getInstance());
    }
    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private long getTime_long(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return  format.parse(time).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }



    @Override
    protected StatisticsTwoPresenter createPresenter() {
        return new StatisticsTwoPresenter(this);
    }


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_statistics;
    }


    @Override
    public RecyclerView getTop() {
        return top;
    }

    @Override
    public StatisticsTwoActivity getAc() {
        return StatisticsTwoActivity.this;
    }

    @Override
    public TextView getStart() {
        return start;
    }

    @Override
    public TextView getEnd() {
        return end;
    }
}
